package com.camera2.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.camera2.dao.UpdateManager;
import com.camera2.http.GlobalUrl;
import com.camera2.local.SharedPrefer;

import java.io.File;

public class ActivitySplash extends Activity {

    private LinearLayout ll_splash;

    private TextView tv_gengxing;
    private ProgressBar proBar;
    public static receiveVersionHandler handler;
    private UpdateManager manager = UpdateManager.getInstance();

    private Handler handler1 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String info = (String) msg.obj;
                    System.out.println("输出的结果" + info);
                    if (info == null) {
                        Intent intent = new Intent(getApplicationContext(),
                                ActivityLogin.class);
                        startActivity(intent);
                        finish();
                    }
                    if (info.equals("成功！")) {
                        ToMain();
                    } else {
                        Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(getApplicationContext(),
                                ActivityLogin.class);
                        startActivity(intent);
                        finish();
                    }
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "连接网络失败,请检查网络是否连接", Toast.LENGTH_SHORT)
                            .show();
                    Intent intent = new Intent(getApplicationContext(),
                            ActivityLogin.class);
                    startActivity(intent);
                    finish();
                    //ToMain();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ll_splash = findViewById(R.id.ll_splash);
        ll_splash.setBackgroundResource(R.drawable.bg2);
        tv_gengxing = findViewById(R.id.tv_gengxing);
        proBar = findViewById(R.id.progressBar_id);
        handler = new receiveVersionHandler();
        init();
    }

    public void init() {

        String id = SharedPrefer.Readid(getApplicationContext());
        if (id.equals("")) {
            ToLogin();
        }

        manager.CheckVersion(this, "ActivitySplash");
    }

    public class receiveVersionHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToMain();
                    break;
                case 1:
                    proBar.setProgress(msg.arg1);
                    proBar.setVisibility(View.VISIBLE);
                    tv_gengxing.setVisibility(View.VISIBLE);
                    if (msg.arg1 == 100) {
                        Intent intent = new Intent("android.intent.action.VIEW");

                        String type = "application/vnd.android.package-archive";

                        File outputImage = new File(GlobalUrl.getSavePath());
                        Uri data;
                        if (Build.VERSION.SDK_INT >= 24) {
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            intent.addCategory("android.intent.category.DEFAULT");
                            data = FileProvider.getUriForFile(getApplicationContext(), "com.camera2.activity.fileprovider", outputImage);
                        } else {
                            intent.addCategory("android.intent.category.DEFAULT");
                            data = Uri.fromFile(outputImage);
                        }
                        intent.setDataAndType(data, type);
                        startActivity(intent);
                    }
                    proBar.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    ToMain();
//                    Toast.makeText(getApplicationContext(), "未检测到版本更新", Toast.LENGTH_LONG)
//                            .show();

            }
        }
    }


    private void ToMain() {
        new Thread(new Runnable() {
            @Override
            public void run() {
//                SystemClock.sleep(1 * 1000);
                Intent intent = new Intent(getApplicationContext(), Frament.class);
                startActivity(intent);
                finish();

            }
        }).start();
    }

    private void ToLogin() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2 * 1000);
                Intent intent = new Intent(getApplicationContext(),
                        ActivityLogin.class);
                startActivity(intent);
                finish();
            }
        }).start();
    }


}
